package com.test.comm.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.comm.anno.WxTable;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author swc
 * @date 2020/5/12 0012 下午 12:24
 */
public class JsonUtil {

    /**
     * 功能描述: 给当前json封装为实体对象
     * @param data
     * @param urlDecode
     * @param tClass
     * @return: T
     * @author: swc
     * @date: 2020/5/12 0012 下午 14:14
     */
    public static <T>T setEntity(String data, boolean urlDecode, Class<T> tClass)throws Exception {
        T entity = null;
        List<T> listEntity = setListEntity(data, urlDecode, tClass);
        if(listEntity!=null && listEntity.size()>0){
            entity =listEntity.get(0);
        }
        return entity;
    }

    /**
     * 功能描述: 设置的实体  json是个对象 非数组和集合
     * @param data
     * @param urlDecode
     * @param tClass
     * @return: T
     * @author: swc
     * @date: 2020/5/12 22:38
     */
    public static <T>T setObjEntity(String data, boolean urlDecode, Class<T> tClass)throws Exception {
        if(urlDecode){
            data = URLDecoder.decode(data,"utf-8");
        }
        JSONObject jsonObject = JSON.parseObject(data);
        return setEntity(jsonObject,tClass);
    }


    /**
     * 功能描述: 给当前json封装为实体对象
     * @param jsonObject
     * @param tClass
     * @return: T
     * @author: swc
     * @date: 2020/5/12 0012 下午 14:14
     */
    private static <T>T setEntity(JSONObject jsonObject, Class<T> tClass)throws Exception {
        T entity = tClass.newInstance();
        Class aClass = entity.getClass();
        Field[] fields = tClass.getDeclaredFields();

        for(Field field:fields){
            WxTable wxTable = field.getAnnotation(WxTable.class);
            if(wxTable!=null){
                getFieldValue(jsonObject.get(wxTable.fieldName()),wxTable,aClass,field,entity);
            }

        }
        return entity;

    }

    /**
     * 功能描述: 给当前json封装为实体对象
     * @param data
     * @param urlDecode
     * @param tClass
     * @return: T
     * @author: swc
     * @date: 2020/5/12 0012 下午 14:14
     */
    public static <T> List<T> setListEntity(String data, boolean urlDecode, Class<T> tClass)throws Exception {
        if(urlDecode){
            data = URLDecoder.decode(data,"utf-8");
        }
        JSONArray jsonArray = JSON.parseArray(data);

        List<T> list =new ArrayList<T>();

        if(jsonArray==null){
            return  list;
        }

        for(int i=0;i<jsonArray.size();i++){
            list.add(setEntity(jsonArray.getJSONObject(i),tClass));
        }
        return list;
    }



    /**
     * 功能描述: 给对象属性设置值
     * @param val1
     * @param wxTable
     * @param aClass
     * @param field
     * @param entity
     * @return: void
     * @author: swc
     * @date: 2020/5/12 0012 下午 14:13
    */

    protected  static  <T>void  getFieldValue(Object val1, WxTable wxTable, Class aClass, Field field, T entity)throws Exception{
        if(val1==null){
            return;
        }
        Method method =null;
        Class source = null;
        if("integer".equalsIgnoreCase(wxTable.type())){
            source =Integer.class;
        }else if("int".equalsIgnoreCase(wxTable.type())){
            source =Integer.class;
        }else if("long".equalsIgnoreCase(wxTable.type())){
            source =Long.class;
        }else if("double".equalsIgnoreCase(wxTable.type())){
            source =Double.class;
        }else if("bigdecimal".equalsIgnoreCase(wxTable.type())){
            source =BigDecimal.class;
        }else if("float".equalsIgnoreCase(wxTable.type())){
            source =Float.class;
        }else if("date".equalsIgnoreCase(wxTable.type())){
            source =Date.class;
        }else if("string".equalsIgnoreCase(wxTable.type())){
            source =String.class;
        }else {
            source =Object.class;
        }

        method = aClass.getMethod("set"+upperFirst(field.getName()),source);
        method.setAccessible(true);

        if(wxTable.removeReg()!=null && !"".equalsIgnoreCase(wxTable.removeReg().trim())){
            String str =(String)val1;
            val1 =str.replaceAll(wxTable.removeReg(),"");
        }
        method.invoke(entity,val1);
    }

    //首字母变大写
    public static String upperFirst(String str){
        char[] chars = str.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char)(chars[0] - 32);
        }
        return new String(chars);
    }


}
